package com.kmzx.one.project.controller;

import com.kmzx.one.common.utils.MyAssert;
import com.kmzx.one.project.net.BaseResult;
import com.kmzx.one.project.others.dto.FatherInfo;
import com.kmzx.one.project.others.dto.PartnerOutInfo;
import com.kmzx.one.project.others.dto.PartnerReqInfo;
import com.kmzx.one.project.others.dto.UserRankInfo;
import com.kmzx.one.project.others.dto.UserRankMoreInfo;
import com.kmzx.one.project.others.dto.UserRankOutInfo;
import com.kmzx.one.project.others.dto.UserRankUPInfo;
import com.kmzx.one.project.service.UserRanksService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;


@Api(tags = "快美甄选_[ 用户_等级_接口 ]")
@RestController
public class UserRanksController extends BaseController {

    @Autowired
    private UserRanksService userRanksService;

    /**
     * @param userId
     * @return
     */
    @ApiOperation(value = "1.用户等级信息", notes = "用户等级信息",position = -1)
    @GetMapping(value = "rank/entry")
    public BaseResult userRankEntry(String userId) {
        try {
            MyAssert.notNullObject(userId, "userId 为null");
            UserRankInfo info = userRanksService.findUserRankTabs(userId);
            BaseResult success = BaseResult.Builder.SUCC().initSuccData(info);
            return success;
        } catch (Exception e) {
            return handleException(e);
        }
    }

    /**
     * 供用户选择合适的
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "2.管家列表", notes = "管家列表")
    @GetMapping(value = "rank/pick")
    public BaseResult pickTopRank(String userId, Integer pageNum, Integer pageSize) {
        try {
            MyAssert.notNullObject(userId, "userId 为null");
            MyAssert.notNullObject(pageNum, "pageNum 为null");
            MyAssert.notNullObject(pageSize, "pageSize 为null");
            List<UserRankMoreInfo> info = userRanksService.findTopRankUser(userId, pageNum, pageSize);
            BaseResult success = BaseResult.Builder.SUCC().initSuccData(info);
            return success;
        } catch (Exception e) {
            return handleException(e);
        }
    }

    /**
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "2-1.管家列表_搜索使用", notes = "2-1.管家列表_搜索使用")
    @GetMapping(value = "rank/pickSearch")
    public BaseResult pickTopRankSearch(String userId, String nickName, Integer pageNum, Integer pageSize) {
        try {
            MyAssert.notNullObject(userId, "userId 为null");
            MyAssert.notNullObject(pageNum, "pageNum 为null");
            MyAssert.notNullObject(pageSize, "pageSize 为null");
            List<UserRankMoreInfo> info = userRanksService.findTopRankUserSearch(userId, nickName, pageNum, pageSize);
            BaseResult success = BaseResult.Builder.SUCC().initSuccData(info);
            return success;
        } catch (Exception e) {
            return handleException(e);
        }
    }

    @ApiOperation(value = "3.确认 选择 管家", notes = "确认 选择 管家")
    @GetMapping(value = "rank/goFor")
    public BaseResult goForRank(String userId, String fromUserId) {
        try {
            MyAssert.notNullObject(userId, "userId 为null");
            MyAssert.notNullObject(fromUserId, "fromUserId 为null");
            beforeAopRepeat(">>>".concat(fromUserId.concat(userId)));
            int info = userRanksService.doUserRankRelation(fromUserId, userId);
            BaseResult success = BaseResult.Builder.SUCC().initSuccData(info);
            return success;
        } catch (Exception e) {
            return handleException(e);
        }
    }

    @ApiOperation(value = "4.我的甄选人", notes = "确认 选择 管家",position = -1)
    @GetMapping(value = "rank/hasFather")
    public BaseResult getFather(String userId) {
        try {
            MyAssert.notNullObject(userId, "userId 为null");
            FatherInfo info = userRanksService.findUserFather(userId);
            BaseResult success = BaseResult.Builder.SUCC().initSuccData(info);
            return success;
        } catch (Exception e) {
            return handleException(e);
        }
    }


    @ApiOperation(value = "5.成长值明细", notes = "成长值明细")
    @GetMapping(value = "rank/detailInfo")
    public BaseResult getRankMsg(String userId, Integer pageNum, Integer pageSize) {
        try {
            MyAssert.notNullObject(userId, "userId 为null");
            pageNum = pageNum == null ? super.pageNum : pageNum;
            pageSize = pageSize == null ? super.pageSize : pageSize;
            UserRankOutInfo info = userRanksService.findUserRankDetailInfo(userId, pageNum, pageSize);
            BaseResult success = BaseResult.Builder.SUCC().initSuccData(info);
            return success;
        } catch (Exception e) {
            return handleException(e);
        }
    }

    @ApiOperation(value = "6.上传partner认证图片", notes = "上传partner认证图片")
    @PostMapping(value = "rank/upImg")
    public BaseResult pushPartnerImg(HttpServletRequest request) {
        try {
            BaseResult success = parentUpload(request);
            return success;
        } catch (IOException e) {
            return handleException(e);
        }
    }


    @ApiOperation(value = "7.partner 认证展示图", notes = "partner 认证展示图")
    @GetMapping(value = "rank/toAuth")
    public BaseResult partnerToAuth(String userId) {
        try {
//            MyAssert.notNullObject(userId, "userId 为null");
            PartnerOutInfo info = userRanksService.findPartnerInfo(userId);
            BaseResult success = BaseResult.Builder.SUCC().initSuccData(info);
            return success;
        } catch (Exception e) {
            return handleException(e);
        }
    }


    @ApiOperation(value = "8.partner 提交认证", notes = "partner 提交认证")
    @PostMapping(value = "rank/partnerAuth")
    public BaseResult partnerDoAuth(PartnerReqInfo request, String userId) {
        try {
            beforeAopRepeat(">>>".concat(request.getCompany()).concat(userId));
            MyAssert.notNullObject(userId, "userId 为null");
            PartnerReqInfo info = userRanksService.partnerDoAuth(request);
            BaseResult success = BaseResult.Builder.SUCC().initSuccData(info);
            return success;
        } catch (Exception e) {
            return handleException(e);
        }
    }

    @ApiOperation(value = "9.用户升级提示", notes = "9.用户升级提示")
    @GetMapping(value = "rank/upWish")
    public BaseResult checkRankNotice(String userId){
        try {
            UserRankUPInfo info = userRanksService.findUserRankUp(userId);
            BaseResult success = BaseResult.Builder.SUCC().initSuccData(info);
            return success;
        } catch (Exception e) {
            return handleException(e);
        }
    }

    @ApiOperation(value = "10.读取升级提醒", notes = "10.读取升级提醒")
    @GetMapping(value = "rank/upRead")
    public BaseResult readRankUpNotice(String userId){
        try {
            userRanksService.readRankUpInfo(userId);
            BaseResult success = BaseResult.Builder.SUCC().initSuccData(200);
            return success;
        } catch (Exception e) {
            return handleException(e);
        }
    }


}
